package dp;

/**
 * @author : Rookie
 * @description : 不同路径 有障碍物
 * @date : 2024/1/14
 */
public class DifferentPathII {

    public static void main(String[] args) {
        // 1是障碍物
        int[][] map = new int[][] {
                {0,0,0,0},
                {0,1,0,0},
                {0,0,1,0},
                {0,0,0,0}
        };
        int pathCount = getPathCount(map);
        System.out.println(pathCount);
    }

    private static int getPathCount(int [][] map) {
        int[][] dp = new int[map.length][map[0].length];
        for (int i = 0; i < map.length && map[i][0] !=1; i++) {
            dp[i][0] = 1;
        }
        for (int i = 0; i < map[0].length && map[0][i] != 1; i++) {
            dp[0][i] = 1;
        }

        for (int i = 1; i < map.length; i++) {
            for (int j = 1; j < map[0].length; j++) {
                if (map[i][j] != 1) {
                    dp[i][j] = dp[i-1][j] + dp[i][j-1];
                }
            }
        }
        Common.print2dArr(dp);
        return dp[dp.length-1][dp[0].length-1];
    }
}
